<template>
  <div class="oolong-select-dropdown" :class="className" :style="styles">
    <slot></slot>
  </div>
</template>
<script>
import {getStyle} from '../../utils/assist';

// const Popper = require('@popperjs/core/dist/umd/popper.js');  // eslint-disable-line
import { createPopper } from '@popperjs/core';

export default {
  name: 'oolongSelectDropdown',
  props: {
    placement: {
      type: String,
      default: 'bottom-start'
    },
    className: {
      type: String
    }
  },
  data() {
    return {
      popper: null,
      width: '',
      popperStatus: false
    };
  },
  computed: {
    styles() {
      let style = {};
      if (this.width) style.width = `${this.width}px`;
      return style;
    }
  },
  methods: {
    update() {
      if (this.popper) {
        this.$nextTick(() => {
          this.popper.update();
          this.popperStatus = true;
        });
      } else {
        this.$nextTick(() => {
          this.popper = createPopper(this.$parent.$refs.reference, this.$el, {
            placement: this.placement,
            modifiers: {
              computeStyle: {
                gpuAcceleration: false
              },
              preventOverflow: {
                boundariesElement: 'window'
              }
            },
            onCreate: () => {
              this.resetTransformOrigin();
              this.$nextTick(this.popper.update());
            },
            onUpdate: () => {
              this.resetTransformOrigin();
            }
          });
        });
      }
      // set a height for parent is Modal and Select's width is 100%
      if (this.$parent.$options.name === 'oolongSelect') {
        this.width = parseInt(getStyle(this.$parent.$el, 'width'));
      }
    },
    destroy() {
      if (this.popper) {
        setTimeout(() => {
          if (this.popper && !this.popperStatus) {
            this.popper.destroy();
            this.popper = null;
          }
          this.popperStatus = false;
        }, 300);
      }
    },
    resetTransformOrigin() {
      // 不判断，Select 会报错，不知道为什么
      if (!this.popper) return;

      let xPlacement = this.popper.popper.getAttribute('x-placement');
      let placementStart = xPlacement.split('-')[0];
      let placementEnd = xPlacement.split('-')[1];
      const leftOrRight = xPlacement === 'left' || xPlacement === 'right';
      if (!leftOrRight) {
        this.popper.popper.style.transformOrigin = placementStart === 'bottom' || (placementStart !== 'top' && placementEnd === 'start') ? 'center top' : 'center bottom';
      }
    }
  },
  created() {
    this.$on('on-update-popper', this.update);
    this.$on('on-destroy-popper', this.destroy);
  },
  beforeDestroy() {
    if (this.popper) {
      this.popper.destroy();
    }
  }
};
</script>
